/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.mainmenu;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GameManager;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.player.SimplePlayer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * @author Massimo Canonaco
 *
 */

public class CreatePlayerProfileDialog extends PlayerInfoDailog {
	/** */
	private ImageIcon noInput;
	
	/** */
	private ImageIcon errorInput;
	
	/** */
	private JTextField profile;

	/** */
	private JLabel errorLabel;

	/** */
	private List<String> existingProfiles;
	
	public CreatePlayerProfileDialog(MainMenuPanel container, Map infoMap, OverallGUILayoutFactory factory) throws IOException 
	{
		super(container,infoMap,factory);
		final String ERROR_COLOR = "dialog.text.errorinfo.color", 
				ERROR_SIZE = "dialog.text.errorinfo.size",
				TEXT_COLOR = "dialog.text.color", 
				TEXT_STROKE_COLOR = "dialog.textstroke.color",
				TEXT_STROKE_SIZE = "dialog.textstroke.size";
		
		Color errorTextColor = factory.getParsedColor((String)infoMap.get(ERROR_COLOR)), 
				strokeColor = factory.getParsedColor((String)infoMap.get(TEXT_STROKE_COLOR)), 
				textColor = factory.getParsedColor((String)infoMap.get(TEXT_COLOR));
		
		float errorSize = Float.parseFloat((String)infoMap.get(ERROR_SIZE)), 
				strokeSize = Float.parseFloat((String)infoMap.get(TEXT_STROKE_SIZE));
		errorLabel = new JLabel();
		backButton.setEnabled(true);
		
		JLabel info = new JLabel(new ImageIcon(factory.createTextImage("Scegli il nome", fontSize, factory.getHeaderFont(), strokeSize, textColor, strokeColor)));
		profile = new JTextField();
		profile.setPreferredSize(new Dimension(250, 22));
		profile.setSize(250, 22);
		add(info, layoutConstraints);
		add(profile, layoutConstraints);
		add(errorLabel,layoutConstraints);
		errorLabel.setVisible(false);
		errorInput = new ImageIcon(factory.createTextImage("Profilo esistente", errorSize, factory.getHeaderFont(), strokeSize, errorTextColor, strokeColor));
		noInput = new ImageIcon(factory.createTextImage("Nome profilo vuoto", errorSize, factory.getHeaderFont(), strokeSize, errorTextColor, strokeColor));

		profile.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) 
			{
				check();
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0)
			{
				check();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {}
		});
	
		add(buttons,layoutConstraints);
	}
		
	/** */
	public void check()
	{
		boolean validProfileName = true;
		playerName = profile.getText();
		if(playerName.isEmpty() || playerName.equals(" "))
		{
			change(false, noInput);
			validProfileName = false;
		}
		else
		{
			for (String es : existingProfiles) {
				if(es.equalsIgnoreCase(playerName))
				{
					change(false, errorInput);
					validProfileName = false;
					break;
				}
			}

		}

		if(validProfileName)
		{
			goButton.setEnabled(true);
			errorLabel.setVisible(false);
		}
	}
	/** */
	public void change(boolean good, ImageIcon errorMessage)
	{
		if(good)
		{
			goButton.setEnabled(true);
			errorLabel.setVisible(false);
		}
		else
		{
			errorLabel.setIcon(errorMessage);
			goButton.setEnabled(false);
			errorLabel.setVisible(true);
		}
	}
	
	@Override
	public void showDialog(Component parent, String title) {
		existingProfiles = getProfilesList();
		super.showDialog(parent, title);
	}
	/** */
	@Override
	public void next() {
		playerName = playerName.toLowerCase();
		GameManager gameManager = parent.getParentMainFrame().getGameManager();
		SimplePlayer newPlayer = gameManager.getClientPlayer();
		newPlayer.setNickname(playerName);
		profile.setText("");
		super.next();
	}
}
